#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    m_pUI(new Ui::Dialog),
    m_dControllerSpeed(10.)
{
    m_pUI->setupUi(this);

    m_pScene = new QGraphicsScene(this);
    m_pUI->graphicsView->setScene(m_pScene);

    m_pUI->graphicsView->setRenderHint(QPainter::Antialiasing);
    m_pScene->setSceneRect(-300,-300,600,400);

    QPen mypenRed = QPen(Qt::red);
    QPen mypenGreen = QPen(Qt::green);

    QLineF TopLine(m_pScene->sceneRect().topLeft(), m_pScene->sceneRect().topRight());
    QLineF LeftLine(m_pScene->sceneRect().topLeft(), m_pScene->sceneRect().bottomLeft());
    QLineF RightLine(m_pScene->sceneRect().topRight(), m_pScene->sceneRect().bottomRight());
    //QLineF BottomLine(m_pScene->sceneRect().bottomLeft(), m_pScene->sceneRect().bottomRight());

    m_pScene->addLine(TopLine,mypenRed);
    m_pScene->addLine(LeftLine,mypenRed);
    m_pScene->addLine(RightLine,mypenRed);
    //m_pScene->addLine(BottomLine,mypenGreen);



    int ItemCount = 1;
    for(int i = 0; i < ItemCount; i++)
    {
        m_pBoxItem = new Ball();
        m_pScene->addItem(m_pBoxItem);
    }
    QPointF MidPoint((m_pScene->sceneRect().bottomLeft()+m_pScene->sceneRect().bottomRight())/2.);
    QPointF LeftTopPoint(MidPoint);
    LeftTopPoint.setX(LeftTopPoint.x()-20.);
    QPointF RightBottomPoint(MidPoint);
    RightBottomPoint.setX(RightBottomPoint.x()+20.);
    RightBottomPoint.setY(RightBottomPoint.y()+10.);
    QPointF RightTopPoint(MidPoint);
    RightTopPoint.setX(RightTopPoint.x()+20.);

    //QRectF rect(LeftTopPoint, RightBottomPoint);
    QLineF line(LeftTopPoint, RightTopPoint);
    m_pControl = m_pScene->addLine(line, mypenRed);
    this->setFocus();

    m_pTimer = new QTimer(this);
    connect(m_pTimer, SIGNAL(timeout()), m_pScene,SLOT(advance()));
    m_pTimer->start(100);
}

Dialog::~Dialog()
{
    delete m_pUI;
}

void Dialog::keyPressEvent(QKeyEvent *evt)
{
    if(evt->key() == Qt::Key_Left)
    {
        QPointF currentPos = m_pControl->pos();
        m_pBoxItem->SetStart(true);
        if(currentPos.x() > -280)
        {
            m_pControl->moveBy(-m_dControllerSpeed++, 0.);
        }
    }
    else if(evt->key() == Qt::Key_Right)
    {
        m_pBoxItem->SetStart(true);
        QPointF currentPos = m_pControl->pos();
        if(currentPos.x() < 280)
        {
            m_pControl->moveBy(+m_dControllerSpeed++, 0.);
        }
    }
    else if(evt->key() == Qt::Key_Home)
    {
        m_pBoxItem->Reset();
    }
    //QPoint currentPos = mapToParent(m_pBoxItem->pos());
    if(m_pBoxItem->y() > 0)
    {
        //this->close();
        //MessageBox(this-> ,"Game", "You missed!!!", 0);
    }
    QDialog::keyPressEvent(evt);
}

void Dialog::keyReleaseEvent(QKeyEvent *evt)
{
    if(!evt->isAutoRepeat())
    {
        m_dControllerSpeed = 10.;
    }
    QWidget::keyReleaseEvent(evt);
}
